using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using Microsoft.Practices.CompositeUI;

namespace Jbe.TestSuite.Infrastructure.Library.Activation
{
    /// <summary>
    /// Monitors a <see cref="UIElement"/> and when it gets the focus activates the
    /// <see cref="WorkItem"/> the <see cref="UIElement"/> is contained in.
    /// </summary>
    public class UIElementActivationService : IUIElementActivationService
    {
        private WorkItem workItem;

        /// <summary>
        /// The <see cref="WorkItem"/> where this service lives.
        /// </summary>
        [ServiceDependency]
        public WorkItem WorkItem
        {
            set { workItem = value; }
        }

        public void UIElementAdded(UIElement uiElement)
        {
            uiElement.GotFocus += UIElementGotFocus;
        }

        public void UIElementRemoved(UIElement uiElement)
        {
            uiElement.GotFocus -= UIElementGotFocus;
        }

        private void UIElementGotFocus(object sender, RoutedEventArgs e)
        {
            workItem.Activate();
        }
    }
}
